package com.easyexcel;

import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.util.HashSet;
import java.util.Set;

public class CommentWriteHandler implements RowWriteHandler {

    /**
     * 需要验证身份
     * 需要进行脱敏的列
     * 表头
     * */
    private final String[] headsMasking = {"mobile","phone","construction_code"};

    @Override
    public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer rowIndex, Integer relativeRowIndex, Boolean isHead) {
        if(null != writeSheetHolder){
            writeSheetHolder.setExcludeColumnFiledNames(getSet());
        }

        if(null != writeTableHolder){
            writeTableHolder.setExcludeColumnFiledNames(getSet());

        }

    }

    @Override
    public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {



        System.err.println(writeSheetHolder.getHead());
        //System.err.println(writeSheetHolder.getClazz().getName());

        System.err.println(isHead);




        if(!isHead){

            //List<List<String>> 表头 或者 class表头
            if(null != writeSheetHolder.getHead() || null != writeSheetHolder.getClazz()){
                if(null != row){
                    Cell cell = row.getCell(0);
                    if(null != cell){
                        cell.setCellValue(this.repaceStr(cell.getStringCellValue()));
                    }

                }

            }

        }


    }

    private String repaceStr(String value){
        if(StringUtils.isNotBlank(value)){
            StringBuffer buf = new StringBuffer();
            for(int i=0;i<value.length();i++){
                buf.append("*");
            }
            return buf.toString();
        }
        return value;
    }

    public Set<String> getSet(){
        Set<String> set = new HashSet();
        set.add("姓名");
        set.add("name");
        return set;
    }



}
